<?php
include "lang.php";
include "../service/util/authenticateClientSession.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <?php
        include "import.php";
        include "../service/ProductService.php";
        ?>
        <title>Gia An</title>
        <script type="text/javascript">
            var dateFormat = "<?php echo $langs["register.inputDOB"]; ?>";
            $(document).ready(function() {
                var inpText = $('#dobText');
                if (inpText.val() != dateFormat) {
                    inpText.css('color', '#000');
                }
                else if (inpText.val() == "") {
                    inpText.val(dateFormat);
                    inpText.css('color', '#86774A');
                }
				
                $.validator.addMethod("validateConfirmPass", function(value) {
                    return (value == $("#newPassword").val());
                }, "<?php echo $langs["validation.notMatchPass"] ?>");
			
                $.validator.addMethod("validateEmail", function(value) {
                    var emailRegex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                    return emailRegex.test(value);
                }, "<?php echo $langs["validation.emailNotMatch"] ?>");
			
                $.validator.addMethod("validateDob", function(value) {
                    var dobRegex = /^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/;
                    if(value == "") return true;
                    else if(value == "<?php echo $langs["register.inputDOB"]; ?>") return true;
                    else return dobRegex.test(value);
                }, "<?php echo $langs["register.dobNotMatch"] ?>");
			
                $("#form").validate({
                    rules: {
                        userName: { required: true },
                        password: { required: true },
                        newPassword: { required: true },
                        confirmPassword: { validateConfirmPass: true },
                        dobText: {validateDob: true},
                        fullName: { required: true },
                        phoneNumber: { required: true },
                        email: { validateEmail: true }
                    },
					
                    messages: {
                        password: "<?php echo $langs["validation"]; ?>",
                        newPassword: "<?php echo $langs["validation"]; ?>",
                        fullName: "<?php echo $langs["validation"]; ?>",
                        phoneNumber: "<?php echo $langs["validation"]; ?>"
                    }
                });
            });
		
            function showHide(){
                if(!$("#check").attr('checked')){
                    $("#tr1").css("display", "none");
                    $("#tr2").css("display", "none");
                    $("#tr3").css("display", "none");
                } else {
                    $("#tr1").css("display", "table-row");
                    $("#tr2").css("display", "table-row");
                    $("#tr3").css("display", "table-row");
                }
            }
        </script>
    </head>
    <body>
        <?php include("floating-panel.php"); ?>
        <div class="wrapper">
            <?php
            include("header.php");
            include("../service/userService.php");
            include("../service/receiptService.php");
            ?>  
            <div class="main_body">
                <div class="content_box no_background">
                    <div id="TabbedPanels1" class="TabbedPanels">
                        <ul class="TabbedPanelsTabGroup">
                            <li class="TabbedPanelsTab" tabindex="0"><?php echo $langs["account.accountInfo"]; ?></li>
                            <li class="TabbedPanelsTab" tabindex="1"><?php echo $langs["account.historyCart"]; ?></li>
                        </ul>
                        <div class="TabbedPanelsContentGroup">
                            <div class="TabbedPanelsContent">
                                <form id="form">
                                    <?php
                                    $result = findUserBy($_SESSION['username'][1]);
                                    $row = ConnectionManager::getInstance()->fetch($result);
                                    ?>
                                    <center>
                                        <div id="msg">
                                            <?php
                                            if (isset($_GET["msg"])) {
                                                $msg = $_GET["msg"];
                                                if ($msg == "ok")
                                                    echo "<label class=\"suc\">" . $langs["account.changeInfoSuccessfully"] . "</label>";
                                                else if ($msg == "no")
                                                    echo "<label class=\"error\">" . $langs["account.changeInfoFailed"] . "</label>";
                                                else if ($msg == "notmatch")
                                                    echo "<label class=\"error\">" . $langs["account.notMatchOldPass"] . "</label>";
                                            }
                                            ?>
                                        </div>
                                    </center>
                                    <table class="register_form">
                                        <tr>
                                            <th align="left"><?php echo $langs["register.username"]; ?>:</th>
                                            <td><input type="text" name="username" id="username" disabled="disabled" value="<?php echo $row["USER_NAME"]; ?>"/></td>
                                        </tr>
                                        <tr>
                                            <th align="left"><?php echo $langs["register.fullName"]; ?>:</th>
                                            <td><input type="text" name="fullName" id="fullName" value="<?php echo $row["USER_FULLNAME"]; ?>"/></td>
                                        </tr>
                                        <tr>
                                            <th align="left"><?php echo $langs["register.dob"]; ?>:</th>
                                            <td>
                                                <input disabled="disabled"	 type="text" name="dobText" id="dobText" value="<?php echo $row["USER_DOB"] != "" ? date_format(date_create($row["USER_DOB"]), "d/m/Y") : ""; ?>" onkeydown="this.style.color='black'" onclick="if (this.value==dateFormat) {this.value='';this.style.color='#86774A';}" onblur="if (this.value=='') {this.value=dateFormat;this.style.color='#86774A';}" style="color: #86774A"/>
                                                <a href="#" id="dob"><img src="../images/layout/calendar-button.gif"></a>
                                            </td>
                                        </tr>
                                        <tr>
                                            <th align="left"><?php echo $langs["register.phoneNumber"]; ?>:</th>
                                            <td><input type="text" name="phoneNumber" id="phoneNumber" value="<?php echo $row["USER_PHONENUMBER"]; ?>"/></td>
                                        </tr>
                                        <tr>
                                            <th align="left"><?php echo $langs["register.email"]; ?>:</th>
                                            <td><input type="text" name="email" id="email" value="<?php echo $row["USER_EMAIL"]; ?>"/></td>
                                        </tr>
                                        <tr>
                                            <th colspan="2"><input type="checkbox" name="check" value="1" id="check" onclick="showHide();"/><?php echo $langs["account.check"]; ?></th>
                                        </tr>
                                        <tr id="tr1">
                                            <th align="left"><?php echo $langs["account.currentPassword"]; ?>:</th>
                                            <td><input type="password" name="pwd" id="pwd" /></td>
                                        </tr>
                                        <tr id="tr2">
                                            <th align="left"><?php echo $langs["account.newPassword"]; ?>:</th>
                                            <td><input type="password" name="newPassword" id="newPassword"/></td>
                                        </tr>
                                        <tr id="tr3">
                                            <th align="left"><?php echo $langs["register.confirmPass"]; ?>:</th>
                                            <td><input type="password" name="confirmPassword" id="confirmPassword"/></td>
                                        </tr>
                                        <tr>
                                            <td colspan="2">
                                                <center>
                                                    <a href="#" class="button right" onclick="resetForm('form');">Nhập Lại</a>
                                                    <a href="#" class="button right" onclick="submitForm('form', '../action/updateUserAccountAction.php', 'post');">Đồng Ý</a>
                                                </center>
                                            </td>
                                        </tr>
                                    </table>
                                </form>
                            </div>
                            <div class="TabbedPanelsContent">
                                <!--start history-->
                                <table cellspacing="0" class="cart_table">
                                    <tr>
                                        <th><?php echo $langs["cart.no"]; ?></th>
                                        <th><?php echo $langs["cart.receiptCode"]; ?></th>
                                        <th><?php echo $langs["cart.receiptDate"]; ?></th>
                                        <th><?php echo $langs["cart.receiptStatus"]; ?></th>
                                        <th><?php echo $langs["cart.receiptAddressAndPhone"]; ?></th>
                                        <th><?php echo $langs["cart.receiptReceiver"]; ?></th>
                                    </tr>
                                    <?php
                                    if (isset($_SESSION['username'])) {
                                        $count = 1;
                                        $user_id = $_SESSION['username'][0];
                                        $receipts = getAllReceiptOfUser($user_id);
                                        if (mysql_num_rows($receipts) > 0) {
                                            while ($receipt = mysql_fetch_array($receipts)) {
                                                ?>
                                                <tr>
                                                    <td><?php echo $count;
                                    $count++; ?></td>
                                                    <td><?php echo $receipt["RECEIPT_CODE"]; ?></td>
                                                    <td><?php echo $receipt["RECEIPT_DATE"]; ?></td>
                                                    <td>
                                                        <?php
                                                        if ($receipt["RECEIPT_STATUS"] == 1) {
                                                            echo $langs["cart.newReceipt"];
                                                        } else if ($receipt["RECEIPT_STATUS"] == 2) {
                                                            echo $langs["cart.confirmedReceipt"];
                                                        } else if ($receipt["RECEIPT_STATUS"] == 3) {
                                                            echo $langs["cart.canceledReceipt"];
                                                        }
                                                        ?></td>
                                                    <td><?php echo $receipt["RECEIPT_ADD"] . "-" . $receipt["RECEIPT_PHONE"]; ?></td>
                                                    <td><?php echo $receipt["RECEIPT_RECEIVER"]; ?></td>
                                                </tr>
                                                <?php
                                                $products = getAllProductOfReceipt($receipt["RECEIPT_ID"]);
                                                ?>
                                                <tr>
                                                    <td>Sản phẩm</td>
                                                    <td>Số lượng</td>
                                                    <td>Giá tiền</td>
                                                </tr>
                                                <?php
                                                while ($product = mysql_fetch_array($products)) {
                                                    $productInfor = mysql_fetch_array(getProduct($product["PRO_ID"]));
                                                    ?>
                                                    <tr>
                                                        <td><?php echo $lang == "EN" ? $productInfor["PRO_NAME_EN"] : $productInfor["PRO_NAME_VN"]; ?></td>
                                                        <td><?php echo $product["REI_QUANTITY"]; ?></td>
                                                        <td><?php echo number_format($product["REI_PRICE"]); ?> VND</td>
                                                    </tr>
                                                    <?php
                                                }
                                            }
                                        }
                                        ?>

                                        <?php
                                    }
                                    ?>
                                </table>
                                <!--end history-->
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <script type="text/javascript">
            var TabbedPanels1 = new Spry.Widget.TabbedPanels("TabbedPanels1");
            showHide();
        </script>
        <?php include("footer.php"); ?>
    </body>
</html>
